﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Core;
using Data;
using Model;

namespace Web.Areas.bas.Controllers
{
    public class CompanyController : Controller
    {
        public ActionResult Index() {
            var model = new {
                idField = "company_code",
                form = new {
                    company_code = "",
                    company_name = "",
                    remark = ""
                },
                urls = new {
                    query = "/api/bas/company",
                    add = "/bas/company/edit/",
                    edit = "/bas/company/edit/",
                    remove = "/api/bas/company/edit"
                },
                resx = new {
                    editTitle = "Edit Company",
                    removeComfirm = "Are you sure you want to delete this record?",
                    removeSuccess = "delete successfully!",
                    removeFailed = "delete failed!"
                }
            };
            return View(model);
        }

        public ActionResult Edit(string id) {
            var data = new CompanyApiController().GetMaster(id);
            var model = new {
                idField = "company_code",
                form = data.form,
                defaultData = new {
                    formMaster = new bas_company() {
                        creation_userid = Commons.UserId+ "",
                        creation_date = Commons.ServerDateTime
                    },
                    rowDep = new bas_department() {
                        department_code = "test"
                    }
                },
                urls = new {
                    edit = "/api/bas/company/edit/",
                    queryDep = "/api/bas/company/getDetailDeps/"
                },
                resx = new {
                    saveSuccess = "Save Successfully!",
                    editSuccess = "Edit Successfully!"
                },
                dataSource = new { },
                setting = new {
                    readonlyFieldsInEditOfDep = new string[] { "department_code" }
                }
            };
            return View(model);
        }
    }

    public class CompanyApiController : ApiController
    {

        bas_companyService service = new bas_companyService();

        public dynamic Get(ReqWrapperQuery req) {
            var xml = @"
<SqlSetting DefaultSortName='company_code' DefaultSortOrder ='asc'>
    <Select>
        A.*
    </Select>
    <From>
        bas_company A
    </From> 
    <Where>
        <Fields>
            <Field Name='company_name' Cp='like'></Field>
            <Field Name='company_code' Cp='like'></Field>
        </Fields>
    </Where>
</SqlSetting>
";
            var ret = service.GetDynamicListWithPaging(req.ToParam(xml));
            return ret;
        }

        public dynamic GetMaster(string id) {
            return new {
                form = service.GetModel(ParamQuery.Instance().AddWhere("company_code", id))
            };
        }

        public dynamic GetDetailDeps(string id) {
            return new bas_departmentService().GetDynamicList(new ParamQuery().AddWhere("company", id).AddWhere(" And isnull(state,0) <> '2'"));
        }

        [System.Web.Http.HttpPost]
        public dynamic Edit(ReqWrapperMasterDetailInsert req) {
            var xml = @"
<SqlSetting>
    <Table>
        bas_company
    </Table>
    <Where>
        <Fields>
            <Field Name='company_code' Cp='equal'></Field>
        </Fields>
    </Where>
    <Children>
        <SqlSetting>
            <Table>
                bas_department
            </Table>
            <JsonName>
                dep
            </JsonName>
            <Where>
                <Fields>
                    <Field Name='department_code' Cp='equal' ></Field>
                    <Field Name='company' Cp='equal'></Field>
                </Fields>
            </Where>
            <Dependency>
                <Field Name='company' DependencyName='company_code'></Field>
            </Dependency>
            <FieldInserts>
                <Field Name='department_code'></Field>
                <Field Name='department_name'></Field>
                <Field Name='remark'></Field>
            </FieldInserts>
            <FieldUpdates>
                <Field Name='remark'></Field>
            </FieldUpdates>
        </SqlSetting>
    </Children>
</SqlSetting>
";
            var para = req.ToParams(xml);
            return service.Edit(para);
        }

        [System.Web.Http.HttpPut]
        public dynamic Edit(ReqWrapperMasterDetailUpdate req) {
            var xml = @"
<SqlSetting>
    <Table>
        bas_company
    </Table>
    <Where>
        <Fields>
            <Field Name='company_code' Cp='equal'></Field>
        </Fields>
    </Where>
    <Children>
        <SqlSetting>
            <Table>
                bas_department
            </Table>
            <JsonName>
                dep
            </JsonName>
            <Where>
                <Fields>
                    <Field Name='department_code' Cp='equal' ></Field>
                    <Field Name='company' Cp='equal'></Field>
                </Fields>
            </Where>
            <Dependency>
                <Field Name='company' DependencyName='company_code'></Field>
            </Dependency>
            <FieldInserts>
                <Field Name='department_code'></Field>
                <Field Name='company'></Field>
                <Field Name='department_name'></Field>
                <Field Name='remark'></Field>
            </FieldInserts>
            <FieldUpdates>
                <Field Name='remark'></Field>
            </FieldUpdates>
        </SqlSetting>
    </Children>
</SqlSetting>
";

            var para = req.ToParams(xml);
            return service.Edit(para);
        }

        [System.Web.Http.HttpDelete]
        public int Edit(string id) {
            return new bas_companyService().Update(new ParamBase().AddFieldUpdate("state", "2").AddWhere("company_code", id));
        }

    }

}